package com.zyzd.custom.widget.cardbagview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zyzd.custom.R;


/**
 * Created by lzy on 2018/5/8 0008.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/5/8 0008
 * description ${TODO}
 */
public class CardBagView extends BaseCardBagView<CardBagBean> {
    public CardBagView(Context context) {
        super(context);
    }

    public CardBagView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CardBagView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 进行内容部分的设置
     *
     * @param contentView
     * @param itemBean
     * @param index
     */
    @Override
    protected void initContentView(View contentView, final CardBagBean itemBean, final int index) {

        TextView tvSubTitle = contentView.findViewById(R.id.tv_sub_title);
        tvSubTitle.setText(itemBean.getTitle());

        contentView.findViewById(R.id.tv_sub_title);

        final RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        recyclerView.setAdapter(new CardBagAdapter(getContext()));
        recyclerView.setVisibility(itemBean.isExpansion() ? VISIBLE : GONE);

        final ImageView ivArrow = contentView.findViewById(R.id.iv_arrow);
        ivArrow.setImageResource(itemBean.isExpansion() ? R.mipmap.ic_arrow_down : R.mipmap.ic_arrow_up);

        ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (itemBean.isExpansion()) {
                    itemBean.setExpansion(false);
                    recyclerView.setVisibility(GONE);
                    ivArrow.setImageResource(R.mipmap.ic_arrow_up);
                    return;
                }

                for (int i = 0; i < mDatas.size(); i++) {
                    mDatas.get(i).setExpansion(i == index);
                    mContentViews.get(i).findViewById(R.id.recyclerView).setVisibility(mDatas.get(i).isExpansion() ? VISIBLE : GONE);
                    ((ImageView) mContentViews.get(i).findViewById(R.id.iv_arrow)).setImageResource(mDatas.get(i).isExpansion() ? R.mipmap.ic_arrow_down : R.mipmap.ic_arrow_up);
                }


            }
        });
    }

//    /**
//     * 当要对icon进行操作时可以重写进行修改
//     * @return
//     */
//    @Override
//    protected void loadIconImage(ImageView ivIcon, CardBagBean itemBean, int index) {
//        Glide.with(getContext())
//                .load("")
//                .override(mIconSize,mIconSize)
//                .bitmapTransform(new CropCircleTransformation(getContext()))
//                .into(ivIcon);
//    }


    @Override
    protected int getContentViewLayoutId() {
        return R.layout.layout_module_card;
    }

}
